<template>
    <div>
        <iview-table
                ref="table"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,events:events,changeSelect:changeSelect}">
        </iview-table>
        <br>
        <iview-page
                :totalSize="pageModel.totalSize"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :pageSizeOpts="pageModel.pageSizeOpts"
                v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
        </iview-page>
        <Modal v-model="showModal" @on-ok="save" ok-text="保存" cancel-text="取消" :closable='false' :mask-closable=true :width="500">
            <p slot="header">
                <span>基本信息</span>
            </p>
            <Row>
                <Col span="22" offset="1">
                    <Form ref="form" :model="formItem" :label-width="80">
                        <FormItem label="治疗医生" prop="doctor">
                            <Input v-model="formItem.doctor" type="text" placeholder="请输入治疗医生"></Input>
                        </FormItem>
                        <FormItem label="治疗内容" prop="content">
                            <Input v-model="formItem.content" type="textarea" :autosize="{minRows: 4,maxRows: 5}" placeholder="请输入治疗内容"></Input>
                        </FormItem>
                    </Form>
                </Col>
            </Row>
        </Modal>
    </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    export default {
        components: {
            iviewTable,
            iviewPage
        },
        data () {
            return {
                showModal: false,
                selectRows: [],
                baseURL: '/user/treat/plan/course/record',
                formItem: {
                    id: '',
                    complaintId: '',
                    content: '',
                    doctor: ''
                },
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/treat/plan/course/record',
                    defaultFilters: [
                        {'field': 'complaint_id', 'value1': '-1', 'operate': '=='}
                    ]
                },
                dataModel: {
                    menuId: 655,
                    columns: [
                        {
                            type: 'selection',
                            width: 60,
                            align: 'center'
                        },
                        {
                            title: '治疗内容',
                            key: 'content',
                            className: 'demo-table-info-column'
                        },
                        {
                            title: '医师',
                            key: 'doctor',
                            width: 200
                        },
                        {
                            title: '添加时间',
                            key: 'creationTime',
                            width: 200,
                            className: 'demo-table-info-column',
                            render: (h, params) => {
                                return this.$moment(params.row.creationTime).format('YYYY年MM月DD日')
                            }
                        }
                    ],
                    order: 'id desc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [5, 10, 20, 50, 100]
                }
            };
        },
        methods: {
            events (item) {
                let path = item.path
                if (path === 'add') {
                    this.add()
                } else if (path === 'edit') {
                    this.edit()
                } else if (path === 'delete') {
                    this.delete()
                } else if (path === 'print') {
                    this.print()
                }
            },
            add () {
               this.showModal = true
            },
            save () {
                this.formItem.patientId = this.$store.state.app.patientId
                this.formItem.complaintId = this.$store.state.app.complaintId
                let url = this.baseURL + '/saveOrUpdate'
                this.$ajax.post(url, this.formItem).then((response) => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.$bus.alert('success', '操作成功')
                        // 刷新列表
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            edit () {
                if (this.selectRows.length === 1) {
                    this.formItem = this.selectRows[0]
                    this.showModal = true
                } else {
                    this.$bus.alert('warning', '请选择一条记录')
                }
            },
            delete () {
                if (this.selectRows.length > 0) {
                    let ids = []
                    for (let row of this.selectRows) {
                        ids.push(row.id)
                    }
                    if (ids.length === 1) {
                        this.doDelete(ids)
                    } else {
                        this.$Modal.confirm({
                            title: '操作提示',
                            content: '<p>确认删除' + ids.length + '条记录?</p>',
                            onOk: () => {
                                this.doDelete(ids)
                            }
                        })
                    }
                } else {
                    this.$bus.alert('warning', '请至少选择一条记录')
                }
            },
            doDelete (ids) {
                let url = this.baseURL + '/delete'
                this.$ajax.post(url, ids).then((response) => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.$bus.alert('success', '删除成功')
                        // 刷新列表
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            print () {
                if (this.selectRows.length > 0) {
                    let ids = []
                    for (let row of this.selectRows) {
                        ids.push(row.id)
                    }
                    let url = this.$store.state.app.WEBROOT + '/user/print/partCourseRecords?ids=' + ids + '&patientId=' + this.$store.state.app.patientId + '&userId=' + this.$store.state.app.loginUser.id
                    window.open(url)
                } else {
                    this.$bus.alert('warning', '请至少选择一条记录')
                }
            },
            refresh () {
                this.queryModel.defaultFilters[0] = {'field': 'complaint_id', 'value1': this.$store.state.app.complaintId, 'operate': '=='}
                this.$refs.table.refresh()
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            }
        }
    }
</script>

<style>
</style>
